package com.hisan.freeride.common.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 创建时间 : 2017/6/30
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：实现ImageView高度自适应
 */
public class AutoScaleHeightImageView extends android.support.v7.widget.AppCompatImageView {
    public AutoScaleHeightImageView(Context context) {
        super(context);
    }
    public AutoScaleHeightImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable drawable = getDrawable();
        if(drawable != null){
            int width = drawable.getMinimumWidth();
            int height = drawable.getMinimumHeight();
            float scale = (float)height/width;
            int widthMeasure = MeasureSpec.getSize(widthMeasureSpec);
            int heightMeasure = (int)(widthMeasure*scale);
            heightMeasureSpec =  MeasureSpec.makeMeasureSpec(heightMeasure, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
